<?php
namespace Home\Controller;
class IndexController extends CommonController {
    public function index(){
        $model=M('album');
        $sql="select * from `Po_album`";
        $list=$model->query($sql);
        foreach($list as $k=>$v)
        {
            $f=self::sub($v['face']).$v['face'];
            $list[$k]['face']='/Public/Uploads/'.$f;
        }
        $this->assign('list',$list);
        $this->display();
    }
    public function addalbum(){
        $album_model=M('album');
        $data['name']=I("post.name");
        $filename=$this->upload();
        $data['face']=$filename;
        $data['addTime']=time();
        $res=$album_model->add($data);
        if ($res)
        {//添加成功
            $this->success('添加相册成功',U('Index/index'),2);
            return false;
        }else{
            $this->error('添加相册失败');
            }
    }

    public function showphoto(){
        //var_dump($_POST);exit;
        $data['aid']=I("post.id");
        //var_dump($data);exit;
        $model=M('photo');
        $list=$model->where($data)->select();
        foreach($list as $k=>$v)
        {
            $f=self::sub($v['name']).$v['name'];
            $list[$k]['name']='/Public/Uploads/'.$f;
        }
        //var_dump($list);exit;
        $this->assign('list',$list);
        $this->assign('id',I("post.id"));
        $this->display();
    }

    public function addphoto(){
        $data['aid']=I("post.id");
        $model=M('photo');
        $filename=$this->upload();
        $data['name']=$filename;
        $data['addTime']=time();
        $res=$model->add($data);
        if ($res)
        {//添加成功
            $this->success('添加图片成功',U('Index/index'),2);
            return false;
        }else{
            $this->error('添加图片失败');
        }
    }
    //文件上传
    public function upload(){
        //实例化上传类
        $upload=new \Think\Upload();
        //设置附件上传大小
        $upload->maxSize=3145728;
        //设置附件上传类型
        $upload->exts=array('jpg','gif','png','jpeg');
        //设置附件上传目录
        $upload->rootPath='./Public/';
        $upload->savePath='./Uploads/';
        $upload->saveName=date('Ymd',time()).uniqid();
        $upload->autoSub=true;
        $upload->subName=date('Y',time()).'/'.date('m',time()).'/'.date('d',time());

        //上传文件,返回成功上传的文件信息数组
        $info=$upload->upload();//
        //echo $upload->getError();
        if (!$info)
        {
            //上传错误提示错误信息
            $this->error($upload->getError());
        }else{
            //上传成功
            return $info['face']['savename'];
        }
    }
}